from openai import OpenAI

class Deepseek:
    def __init__(self):
        self.BASE_URL = "https://api.deepseek.com"
        self.API_KEY = "sk-05c35fa7fa01480e939194ce39e1ad1e"
        self.Model = "deepseek-chat"
        self.context = []  # 用于保存每次对话的上下文
        self.client = OpenAI(api_key=self.API_KEY,
                             base_url=self.BASE_URL
                             )
        self.is_stream = True
        print("deepseek初始化成功......")

    def question(self, question: str):
        """向Deepseek提出问题
        Args:
            question (str): 问题

        Returns:
            str: 回答
        """
        if len(self.context) == 0:
            self.context.append({
                'role': 'system',
                'content': '假设你现在是一位专业的医疗领域的智能问答机器人，你可以根据用户提出的问题作出相应的医疗领域的回答并给出相应的解决方案，你只能回答医疗领域的相关问题，不能回答其他领域的问题！！！'
            })

        self.context.append({
            'role': 'user',
            'content': question
        })

        if self.is_stream:
            answer = self.gpt_api_stream(self.context)
            self.context.append({
                'role': 'assistant',
                'content': answer
            })
            return answer
        else:
            answer = self.gpt_api(self.context)
            self.context.append({
                'role': 'assistant',
                'content': answer
            })
            return answer

    # 非流式响应
    def gpt_api(self, messages: list) -> str:
        """为提供的对话消息创建新的回答
        Args:
            messages (list): 完整的对话消息
        """
        completion = self.client.chat.completions.create(
            model=self.Model,
            messages=messages)
        print("Deepseek: " + completion.choices[0].message.content)
        return completion.choices[0].message.content

    # 流式响应
    def gpt_api_stream(self, messages: list) -> str:
        """为提供的对话消息创建新的回答 (流式传输)
        Args:
            messages (list): 完整的对话消息
        """
        stream = self.client.chat.completions.create(
            model=self.Model,
            messages=messages,
            stream=True,
        )

        buffer_result = ''
        print("Deepseek: ", end='')
        for chunk in stream:
            if chunk.choices[0].delta.content is not None:
                buffer_result += chunk.choices[0].delta.content
                print(chunk.choices[0].delta.content, end='')
        print()  # 打印换行符
        return buffer_result

# client = OpenAI(api_key="sk-05c35fa7fa01480e939194ce39e1ad1e", base_url="https://api.deepseek.com")
#
# response = client.chat.completions.create(
#     model="deepseek-chat",
#     messages=[
#         {"role": "system", "content": "假设你现在是一位专业的医疗领域的智能问答机器人，你可以根据用户提出的问题作出相应的医疗领域的回答并给出相应的解决方案，你只能回答医疗领域的相关问题，不能回答其他领域的问题！！！"},
#         {"role": "user", "content": "Hello"},
#     ],
#     stream=False
# )
if __name__ == "__main__":
    #print(response.choices[0].message.content)
    chat=Deepseek()
    while True:
        question = input("User: ")
        chat.question(question)